package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.constant.SystemRoleAuditAreaEnum;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.NewsTypeMapper;
import com.xueyi.aixihu.reqeust.ComplaintReq;
import com.xueyi.aixihu.reqeust.NewsTypeReq;
import com.xueyi.aixihu.service.NewsTypeService;
import com.xueyi.aixihu.status.SystemCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NewsTypeServiceImpl implements NewsTypeService {
    @Autowired
    private NewsTypeMapper newsTypeMapper;
    @Override
    public List<NewsTypeReq> getList(NewsTypeReq newsTypeReq) {
        newsTypeReq.format();
        List<NewsTypeReq> list= newsTypeMapper.getList(newsTypeReq.getName(),newsTypeReq.getType(),
                newsTypeReq.getBeginTime(),newsTypeReq.getEndTime());
        if (!CollectionUtils.isEmpty(list)){
            for (NewsTypeReq newsTypeReq1:list){
                if (newsTypeReq1.getType()==1){
                    newsTypeReq1.setTypeStr("镇");
                }else {
                    newsTypeReq1.setTypeStr("村/社区");
                }
            }
        }
        return list;
    }

    @Override
    public List<NewsTypeReq> getListById(NewsTypeReq newsTypeReq) throws ParseException {
        newsTypeReq.format();

        //条件查询出指定类型下的文章类型信息列表
        List<NewsTypeReq> list = newsTypeMapper.getListByPid(newsTypeReq.getId(), newsTypeReq.getName(),newsTypeReq.getType(),
                newsTypeReq.getBeginTime(),newsTypeReq.getEndTime(),newsTypeReq.getStartIndex(),newsTypeReq.getLimit());
        if (!CollectionUtils.isEmpty(list)){
            for (NewsTypeReq newsTypeReq1:list){
                if (newsTypeReq1.getType()==1){
                    newsTypeReq1.setTypeStr("镇");
                }else {
                    newsTypeReq1.setTypeStr("村/社区");
                }
            }
        }

        //格式化时间
        for (int i = 0; i <list.size() ; i++) {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            list.get(i).setCreateTime1(simpleDateFormat.format(list.get(i).getCreateTime()));
            list.set(i,list.get(i));
        }

        Integer userMaxRole = DtUsersVo.findCurrentUser().getUserMaxRole();
        SystemRoleAuditAreaEnum instance = SystemRoleAuditAreaEnum.getInstance(userMaxRole);
        if(instance!=null&&!instance.getMANAGE_AUDIT_SYSTEM_TYPE_ID().isEmpty()){
            //根绝角色筛选出指定管理的文章类型信息
            list=list.stream().filter(e-> instance.getMANAGE_AUDIT_SYSTEM_TYPE_ID().contains(e.getId())).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public void deleteById(String id) throws SzException {
        //将传入的字符串解析成数组
        String[] strArray = id.split(",");
        int[] ids = new int[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            ids[i] = Integer.parseInt(strArray[i]);
        }
        int temp=0;
        if (ids.length<=0){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"参数异常");
        }
        for (int i = 0; i < ids.length; i++) {
            //判断是否是顶层id
            Integer pid=newsTypeMapper.getPidById(ids[i]);
            if (pid==0){
                temp=1;//此处标记数据中有无法删除的数据，则无法开始进行删除
                throw new SzException(SystemCode.ERROR,"删除失败，id为"+ids[0]+"是顶层信息不可删除");
            }
        }
        if (temp==0){
            for (int i = 0; i < ids.length; i++) {
                //逻辑删除
                newsTypeMapper.deleteById(ids[i]);
            }
        }
    }

    @Override
    public void createType(NewsTypeReq newsTypeReq) throws SzException {
        newsTypeReq.format();
        //依数据库设计，所有值都不能为空
        if (!StringUtils.hasLength(newsTypeReq.getName()) || newsTypeReq.getType()==null || newsTypeReq.getPid()==null ||
            newsTypeReq.getIsValid() == null){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"传入参数都不能为空");
        }
        newsTypeMapper.createType(newsTypeReq.getName(),newsTypeReq.getType(),newsTypeReq.getPid(),newsTypeReq.getIsValid());
    }

    @Override
    public void updateType(NewsTypeReq newsTypeReq) throws SzException {
        newsTypeReq.format();
        if (newsTypeReq.getId() == null){
            throw new SzException(SystemCode.QUERY_CHECK_ERROR,"id不能为空");
        }
        //查询当前id的pid
        if (newsTypeMapper.getPidById(newsTypeReq.getId()) == 0){
            throw new SzException(SystemCode.ERROR,"此处只能修改非顶层的信息");
        }
        newsTypeMapper.updateType(newsTypeReq.getId(),newsTypeReq.getName(),newsTypeReq.getType(),newsTypeReq.getPid());
    }
}
